package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY E-mail: 
* @date 创建时间：2017年8月7日 下午3:45:10 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main")
public class MainFrameController {
	/**
	 * 初始化主界面
	 * @return
	 */
	@RequestMapping(value="/mainpage")
	public ModelAndView mainpage(HttpServletRequest request){
		HttpSession session = request.getSession();
		Integer adminloginid=(Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&menufatherid=-1";
		List<Map<String,Object>> list = new ArrayList<>();
		String code = "";
		String msg = "";
		try {
			JSONObject json = HttpRequest.sendPost("getadminmenu", param);
			code = json.get("code").toString();
			msg = json.get("msg").toString();
			if("100".equals(code)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONArray jsondata = json.getJSONArray("data");
						for(int i=0;i<jsondata.length();i++) {
							JSONObject data = jsondata.getJSONObject(i);
							Map<String,Object> map = new HashMap<>();
							map.put("menuid", data.get("menuid").toString());
							map.put("menuname", data.get("menuname").toString());
							map.put("menufatherid", data.get("menufatherid").toString());
							map.put("menuurl", data.get("menuurl").toString());
							list.add(map);
						}
					}
				} catch (Exception e) {
					code="-200";
					msg="数据解析错误";
					System.out.println("出错了，错误信息是："+e.getMessage());
				}
			}
		} catch (Exception e) {
			code="-100";
			msg="数据传输错误";
			System.out.println("出错了，错误信息是："+e.getMessage());
		}
		ModelAndView model = new ModelAndView();
		model.addObject("list", list);
		model.setViewName("View/MainFrame/MainPage");
		return model;
	}
	/**
	 * 初始化主界面的首页，即index
	 * @return
	 */
	@RequestMapping(value="/index")
	public ModelAndView index() {
		ModelAndView model = new ModelAndView();
		model.setViewName("View/MainFrame/index");
		return model;
	}
}
